﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

// (c) Copyright Romanian Software Company.
// Last Change: 20.10.2009 - by Ionescu Marius.
// All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Collections;
using System.Data;
using AjaxControlToolkit;

namespace NS_Controls
{
    public class LiteralField : BoundField
    {
        protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
        {
            Literal lit = new Literal();
            lit.ID = "lit";
            lit.DataBinding += new EventHandler(HandleDataBinding);
            cell.Controls.Add(lit);
        }

        public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
        {
            if (cell.HasControls() && cell.Controls[0] is Literal)
            {
                Literal lit = (Literal)cell.Controls[0];
                if (lit.Text != string.Empty)
                    dictionary[this.DataField] = lit.Text;
                else
                    dictionary[this.DataField] = System.DBNull.Value;
            }
        }

        private void HandleDataBinding(object sender, EventArgs e)
        {
            Control ctrl = (Control)sender;

            string val = null;
            if (ctrl.NamingContainer != null)
            {
                object rawVal = GetValue(ctrl.NamingContainer);
                val = (null == rawVal ? null : rawVal.ToString());
            }

            if (ctrl is Literal)
            {
                Literal lit = (Literal)ctrl;
                lit.Text = val;
            }
        }
    }
}
